package com.simple_frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.simple_frame.mapper.UserMapper;
import com.simple_frame.pojo.bo.LoginUser;
import com.simple_frame.pojo.po.User;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Objects;

@Service
public class UserDetailsImpl implements UserDetailsService {

    @Resource
    private final UserMapper userMapper;

    public UserDetailsImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_name", username));

        if (Objects.isNull(user)) {
            throw new InternalAuthenticationServiceException("用户不存在");
        }

        // TODO 替换 null 为 从数据库中查询出的权限列表 可以采用 RBAC模型
        //  什么是RBAC模型？ (https://blog.csdn.net/m0_62006803/article/details/133962328)
        return new LoginUser(user, null);
    }
}
